/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "Config.h"
#include <iostream>
#include <fstream>
#include "core/core.h"


extern "C"{
#include "lauxlib.h"
#include "lualib.h"
}

Config *g_pConfig = NULL;

bool coreInitConfig()
{
	if (g_pConfig) 
		DELETE_POINTER(g_pConfig);
	
	g_pConfig = new Config();
	return true;
}

Config* coreGetConfig()
{
	return g_pConfig;
}

Config::Config(void)
{
	stateName = luaL_newstate();
	luaL_openlibs(stateName);
	coreGetScriptingEngineLua()->readFile(stateName, (coreGetWorkingDirectory() + "/configFunctions.lua").c_str());
}

Config::~Config(void)
{
	contents.clear();
	lua_close(stateName);
}

void Config::Destroy()
{
	DELETE_POINTER(g_pConfig);
}

std::string Config::getString(const std::string &key) const
{
	const char* tempStr = coreGetScriptingEngineLua()->getFieldString(stateName, "config", key.c_str());
	return std::string(tempStr);
}

bool Config::getBoolean(const std::string &key) const
{
	return coreGetScriptingEngineLua()->getFieldBool(stateName, "config", key.c_str());
}

void Config::read(const std::string &filename)
{
	coreGetScriptingEngineLua()->readFile(stateName, filename.c_str());
}


void Config::write(const std::string &filename)
{
	coreGetScriptingEngineLua()->writeTableToFile(stateName, "config", "default.cfg");
	//scripEn->writeTableToFile("config", filename.c_str());
}

void Config::trim(std::string &s)
{
	// Remove leading and trailing whitespace
	static const char whitespace[] = " \n\t\v\r\f";
	s.erase( 0, s.find_first_not_of(whitespace) );
	s.erase( s.find_last_not_of(whitespace) + 1U );
}

bool Config::isDefined(const std::string &key) const
{
	return coreGetScriptingEngineLua()->isTableValue(stateName, "config", key.c_str());
}

void Config::setString(const std::string &entry, const std::string &value)
{
	coreGetScriptingEngineLua()->setFieldString(stateName, "config", entry.c_str(), value);
}
	
void Config::setBoolean(const std::string &entry, bool value)
{
	coreGetScriptingEngineLua()->setFieldBool(stateName, "config", entry.c_str(), value);
}

